package com.zrrd.utils;

/**
 * @author ZhangYK
 * @version 8.0
 * @title ResponseResult
 * @description
 * @create 2024/3/24 19:15
 */
public class ResponseResult<O> {
    //响应给前端的代码，20000添加成功，20001添加失败
    private Integer code;

    //相应给前端的数据
    private O data;

    //响应给前端的消息
    private String message;

    public ResponseResult(Integer code) {
        this.code = code;
    }

    public ResponseResult() {
    }

    public ResponseResult(Integer code, O data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    /**
     * 获取
     * @return code
     */
    public Integer getCode() {
        return code;
    }

    /**
     * 设置
     * @param code
     */
    public void setCode(Integer code) {
        this.code = code;
    }

    /**
     * 获取
     * @return data
     */
    public O getData() {
        return data;
    }

    /**
     * 设置
     * @param data
     */
    public void setData(O data) {
        this.data = data;
    }

    /**
     * 获取
     * @return message
     */
    public String getMessage() {
        return message;
    }

    /**
     * 设置
     * @param message
     */
    public void setMessage(String message) {
        this.message = message;
    }

    public static<T> ResponseResult queryResponse(T data){
        if (data == null)
            return new ResponseResult(ResponseStatusCode.FIND_ERR.getCode(),data, ResponseStatusCode.FIND_ERR.getMessage());
        else
            return new ResponseResult(ResponseStatusCode.FIND_OK.getCode(),data, ResponseStatusCode.FIND_OK.getMessage());
    }
    /**
     * 删除通用响应
     * @param flag
     * @return
     */
    public static ResponseResult removeResponse(boolean flag) {
        if (flag) {
            return new ResponseResult(ResponseStatusCode.DELETE_OK.getCode(), flag, ResponseStatusCode.DELETE_OK.getMessage());

        } else {
            return new ResponseResult(ResponseStatusCode.DELETE_ERR.getCode(), flag, ResponseStatusCode.DELETE_ERR.getMessage());
        }
    }

    /**
     * 添加通用响应
     * @param flag
     * @return
     */
    public static ResponseResult addResponse(boolean flag) {
        if (flag) {
            return new ResponseResult(ResponseStatusCode.SAVE_OK.getCode(), flag, ResponseStatusCode.SAVE_OK.getMessage());

        } else {
            return new ResponseResult(ResponseStatusCode.SAVE_ERR.getCode(), flag, ResponseStatusCode.SAVE_ERR.getMessage());
        }
    }

    /**
     * 修改通用响应
     * @param flag
     * @return
     */
    public static ResponseResult updateResponse(boolean flag) {
        if (flag) {
            return new ResponseResult(ResponseStatusCode.UPDATE_OK.getCode(), flag, ResponseStatusCode.UPDATE_OK.getMessage());

        } else {
            return new ResponseResult(ResponseStatusCode.UPDATE_ERR.getCode(), flag, ResponseStatusCode.UPDATE_ERR.getMessage());
        }
    }

    public String toString() {
        return "ResponseResult{code = " + code + ", data = " + data + ", message = " + message + "}";
    }
}
